package edu.pet.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.pet.DAO.PetDAO;
import edu.pet.entity.*;
public class DoctorServlet extends HttpServlet {


	public DoctorServlet() {
		super();
	}


	public void destroy() {
		super.destroy(); 
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("vetname") == null ? "" : request.getParameter("vetname");
		String speciality = request.getParameter("vetspeciality") == null ? "" : request.getParameter("vetspeciality");
		String type = request.getParameter("type") == null ? "" : request.getParameter("type");
		PetDAO dao = new PetDAO();
		
		if("list".equals(type)){
			List<Vet> vets = dao.listVets();
			request.setAttribute("vets", vets);
			//兽医维护
			this.getServletContext().getRequestDispatcher("/user/manage/VetInfo.jsp").forward(request, response);
		}else{
			//兽医查询
			HttpSession session = request.getSession();
			ArrayList<Vet> vets = new ArrayList<Vet>();
			vets = dao.searchDoctor(name, speciality);
			Iterator<Vet> iter = vets.iterator();
			String msg;
			if(iter.hasNext()){
				msg = "查询成功！";
			}else{
				msg="查询失败，查询结果为空！";
			}
			session.setAttribute("msg", msg);
			session.setAttribute("vets", vets);
			response.sendRedirect("../user/doctorinfo.jsp");
		}
	}

	public void init() throws ServletException {
	}

}
